package com.gwtjs.icustom.controller;


import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.gwtjs.icustom.mapper.UserMapper;
import com.gwtjs.icustom.pojo.User;

@RestController @RequestMapping("/user")
public class UserController {

    @Autowired
    private UserMapper userMapper;
    private final static Logger logger = LoggerFactory.getLogger(UserController.class);

    @GetMapping("/queryUserList/{pageSize}/{pageNo}")
    public List<User> queryUserList(){
        List<User> userList = userMapper.queryUserList();

        for (User u: userList) {
            logger.info("\nuser : ", u.toString());
        }

        return userList;
    }

    @GetMapping("/{id}")
    public User queryUserById(@PathVariable("id") Integer id){

        return userMapper.queryUserById(id);

    }

    /**
     * @apiNote ":8080/user/add" 
     * {
		    "account": "flash8627",
		    "password": "flash8627",
		    "username": "flash8627",
		    "address": "flash8627"
		}
     * @param user
     * @return
     */
    @PostMapping("/add")
    public int addUser(@RequestBody User user){
        logger.info("\nadd user:",user.toString());
        return userMapper.addUser(user);
    }
    
    /**
     * @apiNote ":8080/user/update" 
     * {
		    "id":3,
			"account": "old_8627",
		    "password": "flash8627old",
		    "username": "flash8627old",
		    "address": "flash8627old"
		}
     * @param user
     * @return
     */
    @PutMapping("/update")
    public int updateUser(@RequestBody User user){
        logger.info("\nupdate user:",user.toString());
        return userMapper.updateUser(user);
    }

    /**
     * @apiNote ":8080/user/delete" 
     * {"ids":[1,3]}
     * @param ids
     * @return
     */
    @DeleteMapping("/delete")
    public int batchDeleteUser(@RequestBody List<User> list){
    	logger.info("\ndelete user:",list.toString());
        return userMapper.deleteUser(list);
    }

}
